var g_can_debug = true;
function _debug() {
	if (!g_can_debug)
		return;
	if (typeof (console) != "undefined" && typeof (console.debug) != "undefined" ) {
		console.debug(arguments);
	}
}

function _log() {
	if (!g_can_debug)
		return;
	if (typeof (console) != "undefined") {
		console.log(arguments);
	}
}


var g_province = [];
var g_cities = [];
var g_dists = [];
var g_blank_html = "<option value=''>请选择</option>";
function filterCitys(){
	try{
		if ((typeof g_filter)!="undefined" && g_filter){
			var filterArray = g_filter.split(/\||,/);
			var g_all_dists_new = [];
			for(var i=0; i<g_all_dists.length; i++){
				var valid = true;
				for(var j=0; j<filterArray.length; j++){
					if(filterArray[j]==g_all_dists[i].provinceName 
							||filterArray[j]==g_all_dists[i].cityName 
							||filterArray[j]==g_all_dists[i].dist  ){
						valid = false;
					}
				}
				if(valid){
					g_all_dists_new.push(g_all_dists[i]);
				}
			}
			g_all_dists = g_all_dists_new;
		}
	}catch(e){
		alert('Javascript Error:' + e);
	}
	
}
function initCitys() {
	_debug("initCitys start");
	var dom_province = $("#province_id");
	var dom_city = $("#city_id");
	var dom_dist = $("#dist_id");
	if(dom_province.length<=0){
		_debug("initCitys exit due to no #province_id");
		return;
	}
	filterCitys();
	$.each(g_all_dists, function(idx, item) {
		var needMe = true;
		for ( var i = 0; i < g_province.length; i++) {
			if (g_province[i].provinceCode == item.provinceCode) {
				needMe = false;
				break;
			}
		}
		if (needMe) {
			g_province.push(item);
		}
	});
	_debug("g_province:", g_province);

	$.each(g_all_dists, function(idx, item) {
		var needMe = true;
		for ( var i = 0; i < g_cities.length; i++) {
			if (g_cities[i].provinceCode == item.provinceCode
					&& g_cities[i].cityCode == item.cityCode) {
				needMe = false;
				break;
			}
		}
		if (needMe) {
			g_cities.push(item);
		}
	});
	_debug("g_cities:", g_cities);
	_debug("initCitys end");

	

	var tempHtml = g_blank_html;
	$.each(g_province, function(idx, item) {
		tempHtml += "<option value='" + item.provinceCode + "'>"
				+ item.provinceName + "</option>";
	});
	dom_province.html(tempHtml);
	dom_province.change(function() {
		dom_dist.html(g_blank_html);
		if (dom_province.val() == "") {
			dom_city.html(g_blank_html);
			dom_city.trigger('change');
			return;
		}

		var tempHtml = ""; 
		var tempCount = 0;
		var tempVal = "";
		$.each(g_cities, function(idx, item) {
			if (item.provinceCode == dom_province.val()) {
				tempCount++;
				tempVal=item.cityCode;
				tempHtml += "<option value='" + item.cityCode + "'>"
						+ item.cityName + "</option>";
			}
		});
		if(tempCount>1){
			tempHtml = g_blank_html+tempHtml;
			dom_city.css('color','#333333');
		}else{
			dom_city.css('color','#999999');
		}
		
		dom_city.html(tempHtml);
		dom_city.trigger('change');
	});

	dom_city.change(function() {
		if (dom_city.val() == "") {
			dom_dist.html(g_blank_html);
			return;
		}

		var tempHtml = "";
		var tempCount = 0;
		var tempVal = "";
		$.each(g_all_dists, function(idx, item) {
			var optionIdx = 0;
			if (item.provinceCode == dom_province.val()
					&& item.cityCode == dom_city.val()) {
				tempCount++;
				tempVal=item.dist;
				// removed
				tempHtml += "<option  value='"
						+ tempVal + "'>" + item.dist + "</option>";
			}
		});
		if(tempCount>1){
			tempHtml = g_blank_html+tempHtml;
			dom_dist.css('color','#333333');
		}else{
			dom_dist.css('color','#999999');
		}
		
		dom_dist.html(tempHtml);
	});

}

var g_types1 = [];
var g_types2 = [];
var g_types3 = [];
var g_types_count = 0;
var g_select_array = [ "", "", "" ];
function initGroups() {
	_debug("initGroups start");
	$.each(G_JSON_GROUP, function(idx, item) {
		if (item.GP_TYPE3 != null && $.trim(item.GP_TYPE3) != "") {
			g_types_count = Math.max(g_types_count, 3);
		}

		if (item.GP_TYPE2 != null && $.trim(item.GP_TYPE2) != "") {
			g_types_count = Math.max(g_types_count, 2);
		}

		if (item.GP_TYPE1 != null && $.trim(item.GP_TYPE1) != "") {
			g_types_count = Math.max(g_types_count, 1);
		}
	});

	_debug("g_types_count：", g_types_count);
	if (g_types_count >= 4 || g_types_count <= 0) {
		alert("系统配置出错，暂不支持该产品，请联系网站管理员");
	}

	var dom_group1 = $('#group1_id');
	var dom_group2 = $('#group2_id');
	var dom_group3 = $('#group3_id');
	var dom_GP_PK = $('#GP_PK'); // hidden field

	var shouldChangeWidth = true;
	if( typeof G_ignore_width != "undefined" ){
		shouldChangeWidth = false;
	}
	var temp_width1="205px";
	var temp_width2="100px";
	if(typeof G_WIDTH1 != "undefined"){
		temp_width1 = G_WIDTH1;
	}
	if(typeof G_WIDTH2 != "undefined"){
		temp_width2 = G_WIDTH2;
	}
	
	if (g_types_count == 1) {
		if(shouldChangeWidth){
			dom_group1.css("width",temp_width1);
		}
		dom_group2.hide();
		dom_group3.hide();
	} else if (g_types_count == 2) {
		if(shouldChangeWidth){
			dom_group1.css("width",temp_width2);
			dom_group2.css("width",temp_width2);
		}
		dom_group3.hide();
	}
	

	$.each(G_JSON_GROUP, function(idx, item) {
		var needMe = true;

		$.each(g_types1, function(idx2, item2) {
			if (item2.GP_EXTCODE1 == item.GP_EXTCODE1) {
				needMe = false;
				return false;
			}
		});

		if (needMe) {
			g_types1.push(item);
		}
	});
	_debug("g_types1", g_types1);

	if (g_types_count >= 1) {
		var tempHtml = g_blank_html;
		var addedNameArray = [];
		$.each(G_JSON_GROUP, function(idx, item) {
			if (!mm_contain(addedNameArray, item.GP_TYPE1)) {
				tempHtml += "<option value='" + item.GP_TYPE1 + "'>"
						+ item.GP_TYPE1 + "</option>";
				addedNameArray.push(item.GP_TYPE1);
			}
		});
		dom_group1.html(tempHtml);
	} else {
		alert("产品配置错误，请联系网站管理员");
		return;
	}

	dom_group1.change(function() {
		g_select_array[0] = dom_group1.val();
		if (dom_group1.val() == "") {
			dom_group2.html(g_blank_html);
			dom_group3.html(g_blank_html);
			return;
		}
		if (g_types_count >= 2) {
			var tempHtml = g_blank_html;
			var addedNameArray = [];
			$.each(G_JSON_GROUP, function(idx, item) {
				if (item.GP_TYPE1 == dom_group1.val()) {
					if (!mm_contain(addedNameArray, item.GP_TYPE2)) {
						tempHtml += "<option value='" + item.GP_TYPE2 + "'>"
								+ item.GP_TYPE2 + "</option>";
						addedNameArray.push(item.GP_TYPE2);
					}
				}
			});
			dom_group2.html(tempHtml);
		}
	});

	dom_group2.change(function() {
		g_select_array[1] = dom_group2.val();

		if (dom_group2.val() == "") {
			dom_group3.html(g_blank_html);
			return;
		}

		if (g_types_count >= 3) {
			var tempHtml = g_blank_html;
			$.each(G_JSON_GROUP, function(idx, item) {
				if (item.GP_TYPE1 == dom_group1.val()
						&& item.GP_TYPE2 == dom_group2.val()) {
					tempHtml += "<option value='" + item.GP_TYPE3 + "'>"
							+ item.GP_TYPE3 + "</option>";
				}
			});
			dom_group3.html(tempHtml);
		}
	});
	
	
	dom_group3.change(function() {
		g_select_array[2] = dom_group3.val();
	});
	_debug("initGroups end");
}


//define new rules start
(function($){
	$.validationEngineLanguage.allRules["chinamobile"] = {			
			"regex": /^(13[0-9]|147|15(0|1|2|3|5|6|7|8|9)|18[0-9])\d{8}$/,
			"alertText": "* 请输入11位手机号"
	};
	$.validationEngineLanguage.allRules["chinese"] = {			
			"regex": /^[\u4e00-\u9fa5]+$/,
			"alertText": "* 请输入中文"
	};
	$.validationEngineLanguage.allRules["onlyNumber"] = {			
			"regex": /^[0-9]+$/,
			"alertText": "* 请输入数字"
	};
	$.validationEngineLanguage.allRules["onlyLetNumSpec"] = {
			// Good for database fields
			"regex": /^[0-9a-zA-Z_-]+$/,
			"alertText": "* Only Letters, Numbers, hyphen(-) and underscore(_) allowed"
	};
})(jQuery);
//define new rules end


$(function(){ 
   $("#loading").ajaxStart(function(){
	   $(this).show();
	 });
   
   $("#loading").ajaxStop(function(){
	   $(this).hide();
	 });
}); 